<template>
  <div class="login-container">
    <el-form ref="formRef" :model="form" class="login-form" :rules="rules">
      <div class="title-container">
        <h3 class="title">{{ $t('login.title') }}</h3>
      </div>
      <el-form-item prop="username">
        <svg-icon name="user" class="svg-container"></svg-icon>
        <el-input
          :placeholder="$t('login.usernameText')"
          v-model="form.username"
        />
      </el-form-item>
      <el-form-item prop="password">
        <svg-icon name="password" class="svg-container"></svg-icon>
        <el-input
          :type="passwordType"
          :placeholder="$t('login.passwordText')"
          autocomplete="off"
          v-model="form.password"
        />
        <svg-icon
          :name="passwordType === 'password' ? 'eye' : 'eye-open'"
          @click="changeType"
        ></svg-icon>
      </el-form-item>
      <el-button type="primary" class="login-button" @click="handleLogin">{{
        $t('login.btnTitle')
      }}</el-button>
    </el-form>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useI18n } from 'vue-i18n'
const store = useStore()
const i18n = useI18n()

const form = ref({
  username: 'admin',
  password: '688'
})

// 表单校验
const rules = ref({
  username: [
    {
      required: true,
      // message: '请输入用户名',
      message: i18n.t('login.usernameText'),
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      // message: '请输入密码',
      message: i18n.t('login.passwordText'),
      trigger: 'blur'
    }
  ]
})

const passwordType = ref('password')
const changeType = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}

// 点击登录
const formRef = ref(null)
const handleLogin = () => {
  formRef.value.validate(async (vaild) => {
    if (vaild) {
      store.dispatch('app/login', form.value)
    } else {
      return false
    }
  })
}
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>